package importexport;

import java.io.File;
import java.util.Vector;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Course {
	private String name;
	private String description;
	private Vector<String> tutorTypes;
	private Vector<String> tutors;
	private Vector<String> files;
	private String beginYear;
	private String semester;
	private boolean valid = true;
	private String dir;
	
	public Course(Element course, String dir) {
		this.dir = dir;
		NodeList nodes = course.getElementsByTagName("name");
		Element element = (Element) nodes.item(0);
		if (element != null) {
			name = getContent(element);
		}
		else {
			valid = false;
		}
		nodes = course.getElementsByTagName("description");
		element = (Element) nodes.item(0);
		if (element != null) {
			description = getContent(element);
		}
		nodes = course.getElementsByTagName("tutors");
		element = (Element) nodes.item(0);
		if (element != null) {
			NodeList tutors = element.getElementsByTagName("tutor");
			handleTutors(tutors);
		}
		nodes = course.getElementsByTagName("files");
		element = (Element) nodes.item(0);
		if (element != null) {
			NodeList files = element.getElementsByTagName("file");
			handleFiles(files);
		}
	}
	
	private void handleTutors(NodeList nodes) {
		tutors = new Vector<String>();
		tutorTypes = new Vector<String>();
		for (int i = 0; i < nodes.getLength(); i++) {
			Element tutor = (Element) nodes.item(i);
			if (tutor.getAttribute("type") != null) {
				tutorTypes.add(tutor.getAttribute("type"));
			}
			else {
				valid = false;
			}
			NodeList contact = tutor.getElementsByTagName("contact");
			tutor = (Element) contact.item(0);
			if (tutor != null) {
				tutors.add(getContent(tutor));
			}
			else {
				valid = false;
			}
		}
	}
	
	private void handleFiles(NodeList nodes) {
		files = new Vector<String>();
		for (int i = 0; i < nodes.getLength(); i++) {
			Element file = (Element) nodes.item(i);
			NodeList path = file.getElementsByTagName("path");
			file = (Element) path.item(0);
			if (file != null) {
				File test = new File(dir + "\\" + getContent(file));
				if (!test.exists()) {
					valid = false;
				}
				files.add(getContent(file));
			}
			else {
				valid = false;
			}
		}
	}
	
	private String getContent(Element e) {
		Node child = e.getFirstChild();
		if (child instanceof CharacterData) {
			CharacterData cd = (CharacterData) child;
			return cd.getData();
		}
		return "?";
	}
	
	public boolean isValid() {
		return valid;
	}
	
	public String getName() {
		return name;
	}
	
	public String getDescription() {
		return description;
	}
	
	public int getNumberOfTutors() {
		return tutors.size();
	}
	
	public int getNumberOfFiles() {
		return files.size();
	}
	
	public String getTutor(int index) {
		return tutors.get(index);
	}
	
	public String getTutorType(int index) {
		return tutorTypes.get(index);
	}
	
	public String getFile(int index) {
		return files.get(index);
	}
	
	public String getBeginYear() {
		return beginYear;
	}
	
	public void setBeginYear(String year) {
		beginYear = year;
	}
	
	public String getSemester() {
		return semester;
	}
	
	public void setSemester(String sem) {
		semester = sem;
	}
}
